var copycss = new Clipboard('.cardcopycss');
copycss.on('success', function(e) {
	$(".tip_s").fadeIn();
	setTimeout("$('.tip_s').fadeOut();", 1500);
	e.clearSelection();
});

copycss.on('error', function(e) {
	$(".tip_f").fadeIn();
	setTimeout("$('.tip_f').fadeOut();", 1500);
	e.clearSelection();
});

function fullscreen(url) {
	$(".fullscreen").css("background-image", "url('img/flag/" + url + ".svg')");
	$(".fullscreen").fadeIn();
};

$(".fullscreen").click(function() {
	$(".fullscreen").fadeOut();
});

$(document).ready(function() {
	$(".scccc").css("height", $(window).height() + "px");
	$(".scccc").mCustomScrollbar({
		set_width: false,
		set_height: false,
		horizontalScroll: false,
		scrollInertia: 400,
		scrollEasing: "easeInOutQuart",
		mouseWheel: true,
		autoDraggerLength: true,
		scrollButtons: {
			enable: true,
			scrollType: "continuous",
			scrollSpeed: 15,
			scrollAmount: 40
		},
		advanced: {
			updateOnBrowserResize: true,
			updateOnContentResize: true,
			autoExpandHorizontalScroll: true,
			autoScrollOnFocus: true
		},
		callbacks: {
			onScrollStart: function() {},
			onScroll: function() {},
			onTotalScroll: function() {},
			onTotalScrollBack: function() {},
			onTotalScrollOffset: 0,
			whileScrolling: function() {
				whileScrolling();
			},
			whileScrollingInterval: 30
		}
	});
	
	$("#mCSB_1_scrollbar_vertical").css("height", "calc(" + $(window).height() + "px" + " - 87px)");
	$("#mCSB_1_scrollbar_vertical").css("top", "87px");
	$("#totop").fadeOut();
	$(".cards").load("page/cards.daidr", function(responseTxt, statusTxt) {
		if(statusTxt === "success") {
			$(".tip_s").fadeIn();
			setTimeout("$('.tip_s').fadeOut();", 1500);
			$(".cardgetpng").click(function() {
				var flagname = $(this).next().css("background-image");
				flagname = flagname.replace("url(\"", "");
				flagname = flagname.replace("\")", "");
				flagname = flagname.replace(" ", "");
				downloadSVG(flagname);
			});
		}
		if(statusTxt === "error") {
			$(".tip_f").fadeIn();
			setTimeout("$('.tip_f').fadeOut();", 1500);
		}
	});

	$(".start").load("img/logos/DaiDR.svg");
	setTimeout("$('.start').slideUp('slow');", 7500);
});

function whileScrolling() {
	if($("#mCSB_1_dragger_vertical").css("top").replace("px", "") > 40) {
		$("#totop").fadeIn();
	} else {
		$("#totop").fadeOut();
	}
};

$(window).resize(function() {
	$(".scccc").css("height", $(window).height() + "px");
	$("#mCSB_1_scrollbar_vertical").css("height", "calc(" + $(window).height() + "px" + " - 87px)");
	$("#mCSB_1_scrollbar_vertical").css("top", "87px");
});

$("#totop").click(function() {
	$(".scccc").mCustomScrollbar("scrollTo", "top");
});

function downloadSVG(flagname) {
	var download_link = $("<a>");
	download_link.attr("style", "display:none");
	download_link.attr("href", flagname);
	download_link.attr("download", "");
	$("body").append(download_link);
	var download_link_text = '<span id="download_link_text">download<span>';
	download_link.append(download_link_text);
	$('#download_link_text').click();
	download_link.remove();
};